package com.jjwxc.jwjskandriod.activity;

import android.net.http.SslError;
import android.os.Build;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.config.BaseActivity;

public class WebActivity extends BaseActivity {
    private WebView webView;

    @Override
    public int getContentViewId() {
        return R.layout.activity_web;
    }

    @Override
    public void findView() {
        webView = findViewById(R.id.webview);
    }

    @Override
    public void setListener() {

    }

    @Override
    public void afterCreate() {
        String url = getIntent().getStringExtra("WEB_URL");
        String name = getIntent().getStringExtra("WEB_NAME");
        setTitle(name);
        addWebUrl(url);
    }

    public void addWebUrl(String webUrl) {
        webView.getSettings().setUseWideViewPort(true); //将图片调整到适合webview的大小
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webView.getSettings().setDomStorageEnabled(false);
        webView.getSettings().setSupportZoom(false);
        webView.getSettings().setBuiltInZoomControls(false);
        webView.loadUrl(webUrl);
        e(webUrl);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                if (handler != null) {
                    handler.proceed();//忽略证书的错误继续加载页面内容，不会变成空白页面
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    webView.getSettings()
                            .setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
                }

//                myProgressDialog.dismiss();
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String
                    failingUrl) {
//                myProgressDialog.dismiss();
                super.onReceivedError(view, errorCode, description, failingUrl);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
//                myProgressDialog.dismiss();
            }
        });

    }
}
